﻿package fpc;
import flash.display.DisplayObjectContainer;
import fpc.layouts.ILayoutable;
import fpc.utils.Maths;
import fpc.utils.Herald;

class ALayoutable implements ILayoutable
{	
	public var heraldResize(default, null):Herald<ILayoutable>;
	
	public var heraldMove(default, null):Herald<ILayoutable>;
	
	public function new() 
	{
		heraldResize = new Herald();
		heraldMove = new Herald();
		_x = 0;
		_y = 0;
		_width = 0;
		_height = 0;
		widthPortion = Math.NaN;
		heightPortion = Math.NaN;
		_percentsWidth = Math.NaN;
		_percentsHeight = Math.NaN;
		_alignX = 0;
		_alignY = .5;
	}
	
	public function addTo(parent:DisplayObjectContainer, index = Maths.INT_NULL)
	{
		
	}
	
	public function removeFrom(parent:DisplayObjectContainer)
	{
		
	}
	
	public var x(getX, setX):Float;
	var _x:Float;
	function getX()
	{
		return _x;
	}
	function setX(value)
	{
		return _x = value;
	}
	
	public var y(getY, setY):Float;
	var _y:Float;
	function getY()
	{
		return _y;
	}
	function setY(value)
	{
		return _y = value;
	}
	
	public var width(getWidth, setWidth):Float;
	var _width:Float;
	function getWidth()
	{
		return _width;
	}
	function setWidth(value)
	{
		return _width = value;
	}
	
	public var height(getHeight, setHeight):Float;
	var _height:Float;
	function getHeight()
	{
		return _height;
	}
	function setHeight(value)
	{
		return _height = value;
	}
	
	public var widthPortion(default, null):Float;
	
	public var heightPortion(default, null):Float;
	
	public var percentsWidth(getPercentsWidth, setPercentsWidth):Float;
	var _percentsWidth:Float;
	function getPercentsWidth()
	{
		return _percentsWidth;
	}
	function setPercentsWidth(value)
	{
		_percentsWidth = correctedPercents(value);
		widthPortion = value * .01;
		return _percentsWidth;
	}
	
	public var percentsHeight(getPercentsHeight, setPercentsHeight):Float;
	var _percentsHeight:Float;
	function getPercentsHeight()
	{
		return _percentsHeight;
	}
	function setPercentsHeight(value)
	{
		_percentsHeight = correctedPercents(value);
		heightPortion = value * .01;
		return _percentsHeight;
	}
	
	public var alignX(getAlignX, setAlignX):Float;
	var _alignX:Float;
	function getAlignX()
	{
		return _alignX;
	}
	function setAlignX(value)
	{
		return _alignX = correctedAlign(value);
	}
	
	public var alignY(getAlignY, setAlignY):Float;
	var _alignY:Float;
	function getAlignY()
	{
		return _alignY;
	}
	function setAlignY(value)
	{
		return _alignY = correctedAlign(value);
	}
	
	public var parent(default, setParent):ILayoutable;
	function setParent(value)
	{
		parent = value;
		return value;
	}
	
	public var visible(getVisible, setVisible):Bool;
	function getVisible()
	{
		return true;
	}
	function setVisible(value:Bool)
	{
		return true;
	}
	
	static inline function correctedAlign(value:Float)
	{
		if (Math.isNaN(value))
		{
			value = .5;
		}
		else if (value < 0)
		{
			value = 0;
		}
		else if (value > 1)
		{
			value = 1;
		}
		return value;
	}
	
	static inline function correctedPercents(value:Float)
	{
		if (value < 1)
		{
			value = 1;
		}
		else if (value > 100)
		{
			value = 100;
		}
		return value;
	}
	
	public var alpha(getAlpha, setAlpha):Float;
	function getAlpha()
	{
		return 1.;
	}
	function setAlpha(value)
	{
		return 1.;
	}
}